package com.yw.android.common.api;

import com.google.gson.Gson;
import com.yw.android.common.base.BaseApp;
import com.yw.android.common.utils.LOG;
import com.yw.android.common.utils.TextUtils;

import java.io.File;
import java.util.Objects;

import javax.net.ssl.SSLSocketFactory;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;

/**
 * APiService配置类
 */
public class ApiConf {
    String host = "http://www.baidu.com/";
    /**
     * 读超时长，单位：毫秒
     */
    long readTimeOut = 5 * 1000L;

    /**
     * 写超时长，单位：毫秒
     */
    long writeTimeOut = 5 * 1000L;
    /**
     * 连接时长，单位：毫秒
     */
    long connectTimeOut = 5 * 1000L;
    /**
     * http 公共请求头 防篡改 包体加密
     */
    Interceptor mNetInterceptor;
    /**
     * http header等信息
     */
    Interceptor mLocalInterceptor;
    /**
     * SSL安全性
     */
    SSLSocketFactory mSSLSocketFactory;
    /**
     * 自定义类型转化
     */
    Converter.Factory mCustomConverter;
    /**
     * 缓存文件目录
     */
    private String cacheFileName = "okHttpCache";
    /**
     * 缓存文件最大值
     */
    private long maxCacheSize = 1024 * 1024 * 100;

    private ApiConf() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ApiConf apiConf = (ApiConf) o;
        return Objects.equals(host, apiConf.host);
    }

    @Override
    public int hashCode() {
        return Objects.hash(host);
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

    /**
     * 获取缓存信息
     *
     * @return
     */
    Cache getCacheFile() {
        File cacheFile = new File(BaseApp.getAppContext().getCacheDir(), cacheFileName);
        return new Cache(cacheFile, maxCacheSize);
    }

    /**
     * 是否显示网络请求日志
     *
     * @return
     */
    HttpLoggingInterceptor getLogInterceptor() {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return logInterceptor;
    }

    public static final class ApiConfBuilder {
        String host;
        //读超时长，单位：毫秒
        long readTimeOut;
        //写超时长，单位：毫秒
        long writeTimeOut;
        //连接时长，单位：毫秒
        long connectTimeOut;
        //http header等信息
        Interceptor mLocalInterceptor;
        //SSL安全性
        SSLSocketFactory mSSLSocketFactory;
        // 自定义类型转化
        Converter.Factory mCustomConverter;
        //缓存文件目录
        private String cacheFileName;
        //缓存文件目录
        private long maxCacheSize;

        //http 公共请求头 防篡改 包体加密
        private Interceptor mNetInterceptor;

        public ApiConfBuilder() {
        }

        public static ApiConfBuilder anApiConf() {
            return new ApiConfBuilder();
        }

        public ApiConfBuilder withHost(String host) {
            this.host = host;
            return this;
        }

        public ApiConfBuilder withReadTimeOut(long readTimeOut) {
            this.readTimeOut = readTimeOut;
            return this;
        }

        public ApiConfBuilder withWritrTimeOut(long writeTimeOut) {
            this.writeTimeOut = writeTimeOut;
            return this;
        }

        public ApiConfBuilder withConnectTimeOut(long connectTimeOut) {
            this.connectTimeOut = connectTimeOut;
            return this;
        }

        public ApiConfBuilder withLocalInterceptor(Interceptor mLocalInterceptor) {
            this.mLocalInterceptor = mLocalInterceptor;
            return this;
        }

        public ApiConfBuilder withCacheFileName(String cacheFileName) {
            this.cacheFileName = cacheFileName;
            return this;
        }

        public ApiConfBuilder withSSLSocketFactory(SSLSocketFactory mSSLSocketFactory) {
            this.mSSLSocketFactory = mSSLSocketFactory;
            return this;
        }

        public ApiConfBuilder withMaxCacheSize(long maxCacheSize) {
            this.maxCacheSize = maxCacheSize;
            return this;
        }

        public ApiConfBuilder withNetInterceptor(Interceptor mNetInterceptor) {
            this.mNetInterceptor = mNetInterceptor;
            return this;
        }

        public ApiConfBuilder withCustomConverter(Converter.Factory mCustomConverter) {
            this.mCustomConverter = mCustomConverter;
            return this;
        }

        public ApiConf build() {
            ApiConf apiConf = new ApiConf();
            if (!TextUtils.empty(host))
                apiConf.host = this.host;
            if (mLocalInterceptor != null)
                apiConf.mLocalInterceptor = this.mLocalInterceptor;
            if (connectTimeOut > 0)
                apiConf.connectTimeOut = this.connectTimeOut;
            if (maxCacheSize > 0)
                apiConf.maxCacheSize = this.maxCacheSize;
            if (readTimeOut > 0)
                apiConf.readTimeOut = this.readTimeOut;
            if (writeTimeOut > 0)
                apiConf.writeTimeOut = this.writeTimeOut;
            if (!TextUtils.empty(cacheFileName))
                apiConf.cacheFileName = this.cacheFileName;
            if (mNetInterceptor != null)
                apiConf.mNetInterceptor = this.mNetInterceptor;
            if (mSSLSocketFactory != null)
                apiConf.mSSLSocketFactory = this.mSSLSocketFactory;
            if (mCustomConverter != null)
                apiConf.mCustomConverter = this.mCustomConverter;
            return apiConf;
        }
    }

    public class HttpLogger implements HttpLoggingInterceptor.Logger {
        @Override
        public void log(String message) {
            LOG.d(message);
        }
    }
}
